package io.jt808.proto.cmd;

import io.norland.buffer.AutoBuffer;
import io.norland.annotations.Proto;
import io.norland.proto.IProto;
import lombok.Data;

import java.util.ArrayList;
import java.util.List;

/**
 * 多媒体数据上传应答
 */
@Proto("8800")
@Data
public class JT8800 implements IProto {
    /**
     * 多媒体ID
     */
    private int id;

    /**
     * 重传包总数
     */
    private Byte retransmissionPacketsCount;

    /**
     * 重传包ID列表
     */
    private List<Short> retransmissionPacketsId;

    public byte[] writeToBytes() {
        AutoBuffer buffer = new AutoBuffer();
        buffer.writeInt(id);
        if (retransmissionPacketsCount != null) {
            buffer.writeByte(retransmissionPacketsCount);
            for (Short s : retransmissionPacketsId) {
                buffer.writeShort(s);
            }
        }
        return buffer.array();
    }

    public void readFromBuf(AutoBuffer buffer) {
        id = buffer.readInt();
        retransmissionPacketsCount = buffer.readByte();
        if (!buffer.isReadable()) return;
        retransmissionPacketsId = new ArrayList<>();
        while (buffer.isReadable()) {
            retransmissionPacketsId.add(buffer.readShort());
        }
    }

//    public static void main(String[] args) {
//        byte[] bytes = new byte[]{
//                0x00, 0x00, 0x00, 0x01,
//                0x02,
//                0x01, 0x00,
//                0x00, 0x01
//        };
//        JT8800 jt8800=new JT8800();
//        jt8800.readFromBuf(new AutoBuffer(bytes));
//        System.out.println(new String(bytes).equals(new String(jt8800.writeToBytes())));
//    }
}